package com.gali.leetcode.day13;

/**
 * @Auther: gali
 * @Date: 2022-07-28 16:53
 * @Description:
 */
public class Solution {
    /**
     * 接雨水
     * @param height
     * @return
     */
    public int trap(int[] height) {
        int left = 0, right = height.length - 1;
        int sum = 0, max = 0;// 标记雨水高度
        while (left < right) {
            while (left < right && height[left] <= max) { // 只要小于高度，则代表可以接雨水
                sum += (max - height[left]);
                left++;
            }
            while (left < right && height[right] <= max) {
                sum += (max - height[right]);
                right--;
            }
            max = Math.min(height[left], height[right]);
        }
        return sum;
    }

    /**
     * 字符串相乘
     * @param num1
     * @param num2
     * @return
     */
    public String multiply(String num1, String num2) {
        //特殊情况
        if(num1.equals("0")||num2.equals("0")){
            return "0";
        }
        int m = num1.length();
        int n = num2.length();
        int []arr =new int[m+n];
        for (int i = m-1; i >=0 ; i--) {
            int x =num1.charAt(i)-'0';
            for (int j = n-1; j >=0 ; j--) {
                int y =num2.charAt(j)-'0';
                arr[i+j+1]+=x*y;
            }
        }
        for (int i = m+n+1; i >0 ; i--) {
            arr[i-1]+=arr[i]/10;
            arr[i]=arr[i]%10;
        }
        int index=arr[0]==0?1:0;
        StringBuffer stringBuffer = new StringBuffer();
        while (index < m + n) {
            stringBuffer.append(arr[index]);
            index++;
        }
        return stringBuffer.toString();
    }
}
